﻿using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using WCS.WebRequest;
using System.Collections;
using System.Linq;
using Newtonsoft.Json;
using Service;
using SqlSugar;
using System.Reflection;
using Client;
using System.Net.Sockets;
using System.Windows.Forms;
using WCS.Utils;
using WCS.Controls.RunLog;
using WCS.Entity;
using WCS.Helpers;
using System.Configuration;
using WCS.Enum;
using Sunny.UI;
using Sunny.UI.Win32;
using System.Web.UI.WebControls;
using static System.Collections.Specialized.BitVector32;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using WCS.Entity.MES;
using WCS.Controls;

namespace WcsTask.DevCommTaks
{
    /// <summary>
    /// AGV任务处理实体
    /// </summary>
    public class AGVTask
    {
        private AGVTask() { }

        private static readonly AGVTask agvTask = new AGVTask();

        public static AGVTask _agvTask
        {
            get
            {
                return agvTask;
            }
        }

        /// <summary>
        /// 日志模块
        /// </summary>
        string _modular = "【AGV任务模块】";

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarClient _SqlSugarDapperTran = SqlSugarDapper.GetDbContextTran();

        /// <summary>
        /// 交互步骤集合
        /// </summary>
        public List<WCSInteractiveStep> WCSInteractiveStepList;

        /// <summary>
        /// 缓存站台
        /// </summary>
        public List<string> BufferStation;

        /// <summary>
        /// AGV任务下发接口
        /// </summary>
        readonly string _MES = ConfigurationManager.AppSettings["MES"];

        /// <summary>
        /// 仿真模式
        /// </summary>
        string _simulation = ConfigurationManager.AppSettings["simulation"];

        /// <summary>
        /// 通讯方式
        /// </summary>
        readonly string _AGVConnectType = ConfigurationManager.AppSettings["AGVConnectType"];

        /// <summary>
        /// AGV确认放行
        /// </summary>
        readonly string _ConfirmRelease = ConfigurationManager.AppSettings["ConfirmRelease"];

        /// <summary>
        /// AGV任务下发接口
        /// </summary>
        readonly string _SendTask = ConfigurationManager.AppSettings["SendTask"];

        /// <summary>
        /// token可用时间
        /// </summary>
        int _tokenTime = Convert.ToInt32(ConfigurationManager.AppSettings["tokenTime"]);

        /// <summary>
        /// 地图点位
        /// </summary>
        string _PositionBinding = ConfigurationManager.AppSettings["PositionBinding"];

        /// <summary>
        /// 位置字典
        /// </summary>
        Dictionary<string, string> _Position = new Dictionary<string, string>();

        /// <summary>
        /// 输送线任务集合
        /// </summary>
        public List<WcsTaskMain> wcsTaskMains
        {
            get
            {
                var wcsTaskMains = ProcessingTaskMain._processingTaskMain._wcsTaskMains;

                foreach (var wcsTaskMain in wcsTaskMains)
                {
                    wcsTaskMain.wcsTaskConveyor = _SqlSugarDapper.DB.Queryable<WcsTaskConveyor>()
                        .Where(w => w.Task_Code == wcsTaskMain.Task_Code)
                        .OrderBy(w => w.CreateTime)//根据任务先后顺序排序
                        .First();

                    var list = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>()
                        .Where(w => w.Task_Code == wcsTaskMain.Task_Code)
                        .OrderBy(w => w.indexOrder)//根据任务先后顺序排序
                        .ToList();

                    wcsTaskMain.wcsTaskAGVs = list;
                }
                return wcsTaskMains;
            }
        }

        Token token;

        /// <summary>
        /// AGV接口token
        /// </summary>
        Token _Authorization
        {
            get
            {
                if (token == null)
                {
                    token = new Token
                    {
                        token = "admin123",
                        name = "lx-script",
                        createTime = DateTime.Now
                    };
                }
                else
                {
                    if ((DateTime.Now - token.createTime).TotalHours > _tokenTime)
                    {
                        token.createTime = DateTime.Now;
                    }
                }
                return token;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            WCSInteractiveStepList = _SqlSugarDapper.DB.Queryable<WCSInteractiveStep>().ToList();

            BufferStation = _SqlSugarDapper.DB.Queryable<AreaLocation>().Select(s => s.StationNo).ToList();

            var pos = _PositionBinding.Split(',');
            for (int i = 0; i < pos.Length; i++)
            {
                var loc = pos[i].Split('-');

                _Position.Add(loc[0], loc[1]);
            }

            AGVProcessTask();
        }

        /// <summary>
        /// AGV任务处理
        /// </summary>
        public void AGVProcessTask()
        {
            while (true)
            {
                InteractiveSteps();
                //ClientSocket._clientSocket.ClientSend();
                Thread.Sleep(1000);
            }
        }

        /// <summary>
        /// 主任务拆分AGV任务
        /// </summary>
        /// <param name="wcsTaskMain"></param>
        /// <param name="NextRoute"></param>
        public bool MainTaskSplitAGVTask(WcsTaskMain wcsTaskMain, WcsRoute NextRoute)
        {
            try
            {
                if (wcsTaskMain.QualityTesting != "1")
                {
                    //铸造补货
                    //1.空托缓存-----出库口
                    //2.出库口-----铸造
                    if (wcsTaskMain.ProcessType == ProcessType.缓存区到出库口到铸造.ToString())
                    {
                        //每个出口提升机最多之拆分一个此任务
                        var aa = _Position[wcsTaskMain.Current_Station];

                        var agvTask = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().Where(w => w.Start_Station == aa && w.Task_State != (int)Task_State.任务完成).First();

                        if (agvTask != null)
                        {
                            return false;
                        }

                        //var codeInfo = LocationInfo.GetStationInfo(wcsTaskMain.Replenishment);
                        //if (codeInfo.state != (int)StationState.在库)
                        //{
                        //    RunLog.runLog.Warning($"{_modular}【{wcsTaskMain.Replenishment}】分配站台任务未完成 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                        //    return false;
                        //}
                        var indexOrder = 1;
                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Replenishment,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到空托缓存取空料车",//任务提示
                        };

                        LocationInfo.ChangeStatioState(wcsTaskMain.Replenishment, StationState.出库占用);

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = _Position[wcsTaskMain.Start_Station],//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到出库口取物料",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Current_Station,//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到出库口取物料",//任务提示
                        };

                        //取货后站台给离开完成
                        WcsTaskAGV wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = _Position[wcsTaskMain.Start_Station],//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "取货后站台给离开完成",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV5 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = NextRoute.Next_Station,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到铸造送物料",//任务提示
                        };

                        WcsTaskAGV wcsTaskAGV6 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = (Convert.ToInt32(NextRoute.Next_Station) + 10).ToString(),//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到空托缓存取空料车",//任务提示
                        };
                        var freeStation = LocationInfo.GetCacheBit("4000", 1, wcsTaskMain.MaterialType);

                        if (freeStation == null)
                        {
                            RunLog.runLog.Warning($"{_modular}空托缓存 没有可用库位");

                            _SqlSugarDapper.DB.Deleteable<LocationInfo>()
                                .Where(w => w.StationLocation.Contains("40") && w.MaterialInfo == wcsTaskMain.MaterialType)
                                .ExecuteCommand();

                            RunLog.runLog.Warning($"{_modular}=={wcsTaskMain.MaterialType}空托缓存区已释放库位");
                            return false;
                        }

                        WcsTaskAGV wcsTaskAGV7 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = freeStation,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到出库口取物料",//任务提示
                        };
                        var locationInfo1 = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = freeStation,
                            state = (int)StationState.入库占用,
                        };

                        LocationInfo.AddStatioInfo(locationInfo1);

                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = NextRoute.Next_Station,
                            state = (int)StationState.入库占用,
                        };

                        LocationInfo.AddStatioInfo(locationInfo);

                        _SqlSugarDapperTran.BeginTran();
                        //添加任务
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV3).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV4).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV5).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV6).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV7).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                    }
                    //铸造下线
                    //铸造-----空托缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.铸造到缓存区.ToString())
                    {
                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = 1,//任务执行顺序
                            TaskTips = "到空托缓存取空料车",//任务提示
                        };
                        //LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

                        //空闲站台
                        //var freeStation = LocationInfo.GetCacheBit("4000");
                        //freeStation = "4006";
                        //if (freeStation == null)
                        //{
                        //    RunLog.runLog.Warning($"{_modular}空托缓存 没有可用库位 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                        //    return false;
                        //}
                        //var codeInfo = LocationInfo.GetStationInfo(wcsTaskMain.Start_Station);

                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = wcsTaskMain.Replenishment,
                            state = (int)StationState.入库占用,
                        };

                        LocationInfo.AddStatioInfo(locationInfo);

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Replenishment,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = 2,//任务执行顺序
                            TaskTips = "到出库口取物料",//任务提示
                        };

                        _SqlSugarDapperTran.BeginTran();
                        //添加任务
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                        //铸造补货
                    }
                    //砂芯缓存入库
                    //砂芯缓存-----入库口
                    //入库口-----空托缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.砂芯下线到入库口到缓存区.ToString())
                    {
                        var codeInfo = LocationInfo.GetStationInfo(wcsTaskMain.Start_Station);

                        if (ProcessingTaskMain._processingTaskMain.SandCoreStation.Find(f => f == wcsTaskMain.Start_Station) != null)
                        {

                        }
                        return SandAreaToBuffer(wcsTaskMain);
                        //return true;
                    }
                    //制砂芯换车
                    //砂芯-----入库口
                    //入库口-----空托缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.砂芯换车.ToString())
                    {
                        return SandAreaToBuffer(wcsTaskMain);
                    }
                    //砂芯缓存-----制砂芯
                    else if (wcsTaskMain.ProcessType == ProcessType.砂芯缓存到制砂芯.ToString())
                    {
                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Replenishment = 1,//是否继续生产 、继续的话出发补料信号
                            Task_Type = "out",//任务类型
                            indexOrder = 1,//任务执行顺序
                            TaskTips = "空托缓存取料车",//任务提示
                        };
                        LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.End_Station,//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            Task_Type = "in",//任务类型
                            indexOrder = 2,//任务执行顺序
                            TaskTips = "从空托缓存到入库口取空托盘",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.End_Station,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = 3,//任务执行顺序
                            TaskTips = "从空托缓存到入库口取空托盘",//任务提示
                        };
                        wcsTaskAGV2.Start_Station = _Position[wcsTaskAGV3.Start_Station];
                        var codeInfo = LocationInfo.GetStationInfo(wcsTaskMain.Start_Station);

                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = wcsTaskMain.End_Station,
                            state = (int)StationState.入库占用,
                            BarCode1 = codeInfo.BarCode1,
                            BarCode2 = codeInfo.BarCode2,
                            BarCode1Data = codeInfo.BarCode1Data,
                            BarCode2Data = codeInfo.BarCode2Data,
                        };

                        LocationInfo.AddStatioInfo(locationInfo);

                        _SqlSugarDapperTran.BeginTran();
                        //添加任务
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV3).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                    }
                    //空托缓存---砂芯缓存补料车
                    else if (wcsTaskMain.ProcessType == ProcessType.缓存区到制砂芯缓存区.ToString())
                    {
                        //声明子任务实体 
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = 1,//任务执行顺序
                            TaskTips = "空托缓存取料车",//任务提示
                        };
                        LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.End_Station,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = 2,//任务执行顺序
                            TaskTips = "到砂芯缓存送料车",//任务提示
                        };
                        var getInfo = LocationInfo.GetStationInfo(wcsTaskMain.Start_Station);

                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = wcsTaskMain.End_Station,
                            state = (int)StationState.入库占用,
                            //BarCode1 = getInfo.BarCode1,
                            //BarCode2 = getInfo.BarCode2,
                        };

                        LocationInfo.AddStatioInfo(locationInfo);

                        _SqlSugarDapperTran.BeginTran();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.CommitTran();
                    }
                    //从空托缓存，再入库口，空托缓存 
                    //空托缓存-----入库口
                    //入库口-----空托缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.缓存区到入库口到缓存区.ToString())
                    {
                        var indexOrder = 1;

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "空托缓存取料车",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = "9999",//那个入库口任务少就去那个
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到入库口",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.End_Station,//那个入库口任务少就去那个
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "到入库口",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = "99999",//起始位置
                            ActionType = "O" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "入库口返回空托缓存",//任务提示
                        };

                        //更新入库口前的点位
                        wcsTaskAGV4.Start_Station = wcsTaskAGV2.Start_Station = _Position[wcsTaskAGV3.Start_Station];

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV5 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "入库口返回空托缓存",//任务提示
                        };

                        _SqlSugarDapperTran.BeginTran();
                        //添加任务
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV3).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV4).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV5).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                    }
                    //从空托缓存，再出库口，砂芯缓存 
                    //空托缓存-----出库口
                    //出库口-----砂芯缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.出库口到砂芯缓存区.ToString())
                    {
                        var indexOrder = 1;
                        var codeInfo = LocationInfo.GetStationInfo(wcsTaskMain.Replenishment);
                        if (codeInfo.state != (int)StationState.预占用)
                        {
                            RunLog.runLog.Warning($"{_modular}【{wcsTaskMain.Replenishment}】分配站台任务未完成 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                            return false;
                        }
                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Replenishment,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "空托缓存取料车",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "从空托缓存到入库口取空托盘",//任务提示
                        };
                        wcsTaskAGV2.Start_Station = _Position[wcsTaskMain.Start_Station];

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            Task_Type = "in",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "从入库口到砂芯缓存送料车",//任务提示
                        };

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            Task_Type = "in",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "从入库口到砂芯缓存送料车",//任务提示
                        };
                        wcsTaskAGV4.Start_Station = _Position[wcsTaskMain.Start_Station];

                        //声明子任务实体
                        WcsTaskAGV wcsTaskAGV5 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.End_Station,//起始位置
                            ActionType = "O" + (int)ActionType.下料动作无需对接,
                            Task_Type = "in",//任务类型
                            indexOrder = indexOrder++,//任务执行顺序
                            TaskTips = "从入库口到砂芯缓存送料车",//任务提示
                        };

                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = wcsTaskMain.MaterialType,
                            StationLocation = wcsTaskMain.End_Station,
                            state = (int)StationState.入库占用,
                        };

                        LocationInfo.AddStatioInfo(locationInfo);
                        LocationInfo.ChangeStatioState(wcsTaskMain.Replenishment, StationState.出库占用);

                        _SqlSugarDapperTran.BeginTran();
                        //添加任务
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV3).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV4).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV5).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                    }
                    //质检回库
                    //质检站台-----入库口
                    //入库口-----空托缓存
                    else if (wcsTaskMain.ProcessType == ProcessType.质检到入库口到缓存区.ToString())
                    {
                        //声明子任务实体
                        var wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = wcsTaskMain.Start_Station,//起始位置
                            ActionType = "O" + (int)ActionType.上料动作需要对接,
                            Task_Type = "out",//任务类型
                            indexOrder = 1,//任务执行顺序
                            TaskTips = "到制砂芯站台取物料",//任务提示
                        };
                        LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

                        var wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                        {
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            Start_Station = "9999",
                            indexOrder = 2,//任务执行顺序
                            TaskTips = "从制砂芯到入库口",
                        };

                        var wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
                        {
                            ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                            Start_Station = wcsTaskMain.End_Station,//那个入库口任务少就去那个
                            indexOrder = 3,//任务执行顺序
                            TaskTips = "从制砂芯到入库口",
                        };

                        //更新入库口前的点位
                        wcsTaskAGV2.Start_Station = _Position[wcsTaskAGV3.Start_Station];

                        //空闲站台
                        var freeStation = LocationInfo.GetCacheBit("4000", 0, wcsTaskMain.MaterialType);
                        if (freeStation == null)
                        {
                            RunLog.runLog.Warning($"{_modular}空托缓存 没有可用库位 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                            return false;
                        }
                        var locationInfo = new LocationInfo
                        {
                            MaterialInfo = MaterialType.空.ToString(),
                            StationLocation = freeStation,
                            state = (int)StationState.入库占用,
                        };

                        //声明子任务实体
                        var wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
                        {
                            Start_Station = freeStation,//起始位置
                            ActionType = "O" + (int)ActionType.移动任务,
                            Task_Type = "in",//任务类型
                            indexOrder = 4,//任务执行顺序
                            TaskTips = "从入库口到空托缓存",//任务提示
                        };
                        LocationInfo.AddStatioInfo(locationInfo);

                        _SqlSugarDapperTran.BeginTran();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV3).ExecuteCommand();
                        _SqlSugarDapperTran.Insertable(wcsTaskAGV4).ExecuteCommand();

                        _SqlSugarDapperTran.CommitTran();
                    }
                    else
                    {
                        RunLog.runLog.Warning($"{_modular}任务流程未定义 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】 ");//分配库位成功【{wcsTaskAGV.WMSBinCode}】");
                        return false;
                    }
                }
                else
                {
                    //声明子任务实体
                    WcsTaskAGV wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
                    {
                        Start_Station = wcsTaskMain.Start_Station,//起始位置
                        ActionType = "O" + (int)ActionType.上料动作需要对接,
                        Replenishment = wcsTaskMain.ContinueProduction,//是否继续生产 、继续的话出发补料信号
                        indexOrder = 1,//任务执行顺序
                        TaskTips = "到制砂芯站台取物料",//任务提示
                    };
                    LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

                    //空闲站台
                    var freeStation = LocationInfo.GetCacheBit("5000", 0, wcsTaskMain.MaterialType);
                    if (freeStation == null)
                    {
                        RunLog.runLog.Warning($"{_modular}空托缓存 没有可用库位 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                        return false;
                    }
                    var getInfo = LocationInfo.GetStationInfo(wcsTaskMain.Start_Station);
                    var locationInfo = new LocationInfo
                    {
                        MaterialInfo = wcsTaskMain.MaterialType,
                        StationLocation = freeStation,
                        state = (int)StationState.入库占用,
                        BarCode1 = getInfo.BarCode1,
                        BarCode2 = getInfo.BarCode2,
                    };
                    LocationInfo.AddStatioInfo(locationInfo);

                    var wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
                    {
                        Start_Station = freeStation,
                        Task_Type = "in",//任务类型
                        ActionType = "O" + (int)ActionType.移动任务,
                        indexOrder = 2,//任务执行顺序
                        TaskTips = "从制砂芯到入库口",
                    };

                    _SqlSugarDapperTran.BeginTran();
                    _SqlSugarDapperTran.Insertable(wcsTaskAGV1).ExecuteCommand();
                    _SqlSugarDapperTran.Insertable(wcsTaskAGV2).ExecuteCommand();

                    _SqlSugarDapperTran.CommitTran();
                }
                RunLog.runLog.Debug($"{_modular}任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】 ");//分配库位成功【{wcsTaskAGV.WMSBinCode}】");

                return true;
            }
            catch (Exception ex)
            {
                _SqlSugarDapperTran.RollbackTran();
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
                return false;
            }
        }

        /// <summary>
        /// 砂芯区到入库（喷涂点）
        /// </summary>
        /// <param name="wcsTaskMain"></param>
        /// <returns></returns>
        //public bool SandAreaToBuffer(WcsTaskMain wcsTaskMain)
        //{
        //    var indexOrder = 1;
        //    //声明子任务实体
        //    var wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
        //    {
        //        Start_Station = wcsTaskMain.Start_Station,//起始位置
        //        ActionType = "O" + (int)ActionType.上料动作需要对接,
        //        Task_Type = "out",//任务类型
        //        Replenishment = 1,//是否继续生产 、继续的话出发补料信号
        //        indexOrder = indexOrder++,//任务执行顺序
        //        TaskTips = "到制砂芯站台取物料",//任务提示
        //    };
        //    LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

        //    //var wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
        //    //{
        //    //    ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
        //    //    Start_Station = "8888",
        //    //    indexOrder = indexOrder++,//任务执行顺序
        //    //    TaskTips = "从制砂芯到入库口前申请进入",
        //    //};

        //    //var wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
        //    //{
        //    //    ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
        //    //    //Start_Station = GetStoragePortTask("in"),//那个入库口任务少就去那个
        //    //    Start_Station = wcsTaskMain.End_Station,//那个入库口任务少就去那个
        //    //    indexOrder = indexOrder++,//任务执行顺序
        //    //    TaskTips = "从制砂芯到入库口",
        //    //};

        //    //var wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
        //    //{
        //    //    ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
        //    //    Start_Station = "A111",//GetStoragePortTask("in"),//那个入库口任务少就去那个
        //    //    indexOrder = indexOrder++,//任务执行顺序
        //    //    TaskTips = "入库完成后给AGV离开站台",
        //    //};

        //    ////更新入库口前的点位
        //    //wcsTaskAGV4.Start_Station = wcsTaskAGV2.Start_Station = _Position[wcsTaskAGV3.Start_Station];

        //    //空闲站台
        //    //var freeStation = LocationInfo.GetCacheBit("4000", 0, wcsTaskMain.MaterialType);
        //    //freeStation = "4008";
        //    var ss= _SqlSugarDapper.DB.Queryable<LocationInfo>()
        //        .Where(w => w.StationLocation.Contains("800"))
        //        .ToList();
        //    if (ss.Count>=2)
        //    {
        //        _SqlSugarDapper.DB.Deleteable<LocationInfo>()
        //            .Where(w => w.StationLocation.Contains("800"))
        //            .ExecuteCommand();
        //    }
        //    var freeStation = GetCacheBit("8000",1);

        //    if (freeStation == null)
        //    {
        //        RunLog.runLog.Warning($"{_modular}砂芯入库喷涂点 没有可用库位 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
        //        return false;
        //    }
        //    var locationInfo = new LocationInfo
        //    {
        //        MaterialInfo = MaterialType.空.ToString(),
        //        StationLocation = freeStation,
        //        state = (int)StationState.入库占用,
        //    };

        //    //声明子任务实体
        //    var wcsTaskAGV5 = new WcsTaskAGV(wcsTaskMain)
        //    {
        //        Start_Station = freeStation,//起始位置
        //        ActionType = "O" + (int)ActionType.下料动作无需对接,
        //        Task_Type = "in",//任务类型
        //        indexOrder = indexOrder++,//任务执行顺序
        //        TaskTips = "从入库口到空托缓存",//任务提示
        //    };
        //    LocationInfo.AddStatioInfo(locationInfo);

        //    _SqlSugarDapper.DB.BeginTran();
        //    _SqlSugarDapper.DB.Insertable(wcsTaskAGV1).ExecuteCommand();
        //    //_SqlSugarDapper.DB.Insertable(wcsTaskAGV2).ExecuteCommand();
        //    //_SqlSugarDapper.DB.Insertable(wcsTaskAGV3).ExecuteCommand();
        //    //_SqlSugarDapper.DB.Insertable(wcsTaskAGV4).ExecuteCommand();
        //    _SqlSugarDapper.DB.Insertable(wcsTaskAGV5).ExecuteCommand();

        //    _SqlSugarDapper.DB.CommitTran();
        //    return true;
        //}

        /// <summary>
        /// 砂芯区到入库
        /// </summary>
        /// <param name="wcsTaskMain"></param>
        /// <returns></returns>
        public bool SandAreaToBuffer(WcsTaskMain wcsTaskMain)
        {
            var indexOrder = 1;
            //声明子任务实体
            var wcsTaskAGV1 = new WcsTaskAGV(wcsTaskMain)
            {
                Start_Station = wcsTaskMain.Start_Station,//起始位置
                ActionType = "O" + (int)ActionType.上料动作需要对接,
                Task_Type = "out",//任务类型
                Replenishment = 1,//是否继续生产 、继续的话出发补料信号
                indexOrder = indexOrder++,//任务执行顺序
                TaskTips = "到制砂芯站台取物料",//任务提示
            };
            LocationInfo.ChangeStatioState(wcsTaskMain.Start_Station, StationState.出库占用);

            var wcsTaskAGV2 = new WcsTaskAGV(wcsTaskMain)
            {
                ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                Start_Station = "8888",
                indexOrder = indexOrder++,//任务执行顺序
                TaskTips = "从制砂芯到入库口前申请进入",
            };

            var wcsTaskAGV3 = new WcsTaskAGV(wcsTaskMain)
            {
                ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                //Start_Station = GetStoragePortTask("in"),//那个入库口任务少就去那个
                Start_Station = wcsTaskMain.End_Station,//那个入库口任务少就去那个
                indexOrder = indexOrder++,//任务执行顺序
                TaskTips = "从制砂芯到入库口",
            };

            var wcsTaskAGV4 = new WcsTaskAGV(wcsTaskMain)
            {
                ActionType = "E" + (int)ActionType.移动到某个点等待上层系统确认上层放行,
                Start_Station = "A111",//GetStoragePortTask("in"),//那个入库口任务少就去那个
                indexOrder = indexOrder++,//任务执行顺序
                TaskTips = "入库完成后给AGV离开站台",
            };

            //更新入库口前的点位
            wcsTaskAGV4.Start_Station = wcsTaskAGV2.Start_Station = _Position[wcsTaskAGV3.Start_Station];

            //空闲站台
            //var freeStation = LocationInfo.GetCacheBit("4000", 0, wcsTaskMain.MaterialType);
            //freeStation = "4008";
            var freeStation = GetFreeStation();

            if (freeStation == null)
            {
                RunLog.runLog.Warning($"{_modular}空托缓存 没有可用库位 任务号:【{wcsTaskMain.Task_Code}】托盘号:【{wcsTaskMain.TrayBar_Code}】");
                return false;
            }
            var locationInfo = new LocationInfo
            {
                MaterialInfo = MaterialType.空.ToString(),
                StationLocation = freeStation,
                state = (int)StationState.入库占用,
            };

            //声明子任务实体
            var wcsTaskAGV5 = new WcsTaskAGV(wcsTaskMain)
            {
                Start_Station = freeStation,//起始位置
                ActionType = "O" + (int)ActionType.下料动作无需对接,
                Task_Type = "in",//任务类型
                indexOrder = indexOrder++,//任务执行顺序
                TaskTips = "从入库口到空托缓存",//任务提示
            };
            LocationInfo.AddStatioInfo(locationInfo);

            _SqlSugarDapper.DB.BeginTran();
            _SqlSugarDapper.DB.Insertable(wcsTaskAGV1).ExecuteCommand();
            _SqlSugarDapper.DB.Insertable(wcsTaskAGV2).ExecuteCommand();
            _SqlSugarDapper.DB.Insertable(wcsTaskAGV3).ExecuteCommand();
            _SqlSugarDapper.DB.Insertable(wcsTaskAGV4).ExecuteCommand();
            _SqlSugarDapper.DB.Insertable(wcsTaskAGV5).ExecuteCommand();

            _SqlSugarDapper.DB.CommitTran();
            return true;
        }

        /// <summary>
        /// 处理AGV交互步骤
        /// </summary>
        private void InteractiveSteps()
        {
            try
            {
                foreach (var wcsTaskMain in ProcessingTaskMain._processingTaskMain._wcsTaskMains.FindAll(f => f.wcsTaskAGVs.Count != 0))
                {
                    //等待删除的任务
                    var pssTaskList = wcsTaskMain.wcsTaskAGVs.Last();
                    if (pssTaskList.Task_State == (int)Task_State.任务完成)
                    {
                        //触发返空任务
                        //if (pssTaskList.Start_Station.Contains("30") && pssTaskList.TaskTips == "到铸造送物料")
                        //{
                        //    _SqlSugarDapper.DB.Insertable(new CallMaterial
                        //    {
                        //        CallStation = pssTaskList.Start_Station,
                        //        MaterialType = pssTaskList.materialType,
                        //        isSupply = 1,
                        //        SupplyNum = 2
                        //    }).ExecuteCommand();
                        //}

                        AGVTaskCompleteAll(pssTaskList);

                        if (pssTaskList.Task_Type != "in" && pssTaskList.Task_Type != "out")
                        {
                            LocationInfo.ChangeStatioState(pssTaskList.Start_Station, StationState.预占用, "空");

                            RunLog.runLog.Debug($"{_modular}【{wcsTaskMain.Replenishment}】分配站台任务完成 任务号:【{wcsTaskMain.Task_Code}】");
                        }
                        continue;
                    }

                    //筛选出执行中的任务
                    var wcsTaskAGV = wcsTaskMain.wcsTaskAGVs.Find(p => p.Task_State == (int)Task_State.执行中);

                    if (wcsTaskAGV != null)
                    {
                        //根据AGV的交互站台 判断PLC站台状态是否可用
                        var activeStation = PlcDev._plcDev.ConveyorStationList.Find(f => f.Station_Plc_No == wcsTaskAGV.Start_Station);

                        //发送给AGV允许进入信号
                        var sendInteractive = WCSInteractiveStepList.Find(w => w.CurrentState == wcsTaskAGV.InteractiveState && w.InteractiveType == "Send");
                        if (sendInteractive != null)
                        {
                            //发送数据
                            if (SendInteractive(wcsTaskAGV, activeStation, sendInteractive))
                            {
                                if (sendInteractive.TargetState == 50)
                                {
                                    _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                          .SetColumns(w => w.Task_State == (int)Task_State.任务完成)
                                          .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                          .ExecuteCommand();
                                }
                                else
                                {
                                    _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                        .SetColumns(w => w.InteractiveState == sendInteractive.TargetState)
                                        .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                        .ExecuteCommand();
                                }

                                RunLog.runLog.Debug($"{_modular}-----任务号【{wcsTaskAGV.Task_Code}】【{wcsTaskAGV.Start_Station}】更新步骤【{wcsTaskAGV.InteractiveState}】 ===> 【{sendInteractive.TargetState}】【{sendInteractive.Remarks}】");
                            }

                            if (sendInteractive.finish == "Finish")
                            {
                                //更新子任务当前位置
                                //更任务的任务提示
                                _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                      .SetColumns(w => w.Task_State == (int)Task_State.任务完成)
                                      .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                      .ExecuteCommand();
                            }
                        }

                        //接受AGV消息
                        var agvReceiveInteractive = WCSInteractiveStepList.Find(w => w.SendState == wcsTaskAGV.ReceiveInteractiveState && w.CurrentState == wcsTaskAGV.InteractiveState && w.InteractiveType == "ReceiveInteractive" && w.InteractiveDev == "AGV");
                        if (agvReceiveInteractive != null)
                        {
                            var wcsTaskAGV1 = wcsTaskMain.wcsTaskAGVs.Find(p => p.indexOrder == wcsTaskAGV.indexOrder + 1);

                            if (wcsTaskAGV1 != null && (wcsTaskAGV1.Start_Station.Contains("A00") || wcsTaskAGV1.Start_Station.Contains("B00")))
                            {
                                bool success = false;
                                //判断PLC站台是否可以进去
                                if (wcsTaskAGV1.Start_Station == "A001" && PlcDev._plcDev.conveyorReadData.AllowAOne)
                                {
                                    //告诉AGV允许进入
                                    var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                                    if (result.errCode == "Err")
                                    {
                                        RunLog.runLog.Warning($"{_modular}下发任务失败,AGV任务编号{wcsTaskAGV.AGVTaskID} 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                        continue;
                                    }
                                    success = true;
                                }

                                //判断PLC站台是否可以进去
                                if (wcsTaskAGV1.Start_Station == "A007" && PlcDev._plcDev.conveyorReadData.AllowASeven)
                                {
                                    //告诉AGV允许进入
                                    var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                                    if (result.errCode == "Err")
                                    {
                                        RunLog.runLog.Warning($"{_modular}下发任务失败,AGV任务编号{wcsTaskAGV.AGVTaskID} 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                        continue;
                                    }
                                    success = true;
                                }

                                //判断PLC站台是否可以进去
                                if (wcsTaskAGV1.Start_Station == "B001" && PlcDev._plcDev.conveyorReadData.AllowBOne)
                                {
                                    //告诉AGV允许进入
                                    var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                                    if (result.errCode == "Err")
                                    {
                                        RunLog.runLog.Warning($"{_modular}下发任务失败,AGV任务编号{wcsTaskAGV.AGVTaskID} 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                        continue;
                                    }
                                    success = true;
                                }

                                if (wcsTaskAGV1.Start_Station == "B007" && PlcDev._plcDev.conveyorReadData.AllowBSeven)
                                {
                                    //告诉AGV允许进入
                                    var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                                    if (result.errCode == "Err")
                                    {
                                        RunLog.runLog.Warning($"{_modular}下发任务失败,AGV任务编号{wcsTaskAGV.AGVTaskID} 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                        continue;
                                    }
                                    success = true;
                                }

                                if (success)
                                {
                                    RunLog.runLog.Debug($"{_modular}-----任务号【{wcsTaskAGV.Task_Code}】【{wcsTaskAGV1.Start_Station}】 允许AGV进入！！AGV任务编号{wcsTaskAGV.AGVTaskID}");
                                    _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                          .SetColumns(w => w.Task_State == (int)Task_State.任务完成)
                                          .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                          .ExecuteCommand();
                                }
                                continue;
                            }
                            else if (wcsTaskAGV1 != null && _Position.ContainsValue(wcsTaskAGV.Start_Station))
                            {
                                //AGV离开完成
                                _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                    .SetColumns(w => w.InteractiveState == 48)
                                    .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                    .ExecuteCommand();

                                continue;
                            }
                            _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                .SetColumns(w => w.InteractiveState == agvReceiveInteractive.TargetState)
                                .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                .ExecuteCommand();

                            RunLog.runLog.Debug($"{_modular}-----任务号【{wcsTaskAGV.Task_Code}】【{wcsTaskAGV.Start_Station}】更新步骤【{wcsTaskAGV.InteractiveState}】 ===> 【{agvReceiveInteractive.TargetState}】【{agvReceiveInteractive.Remarks}】");
                        }

                        //判断PLC状态
                        var plcReceiveInteractive = WCSInteractiveStepList.Find(w => w.CurrentState == wcsTaskAGV.InteractiveState && w.InteractiveType == "ReceiveInteractive" && w.InteractiveDev == "PLC");
                        if (plcReceiveInteractive != null)
                        {
                            if (ReceiveInteractive(wcsTaskAGV, activeStation, plcReceiveInteractive))
                            {
                                _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                    .SetColumns(w => w.InteractiveState == plcReceiveInteractive.TargetState)
                                    .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                    .ExecuteCommand();

                                RunLog.runLog.Debug($"{_modular}-----任务号【{wcsTaskAGV.Task_Code}】【{wcsTaskAGV.Start_Station}】更新步骤【{wcsTaskAGV.InteractiveState}】 ===> 【{plcReceiveInteractive.TargetState}】【{plcReceiveInteractive.Remarks}】");
                            }
                        }

                        if (wcsTaskAGV.ReceiveInteractiveState == 50)
                        {
                            //是否是制砂芯站台
                            if (ProcessingTaskMain._processingTaskMain.SandCoreStation.Find(f => f == wcsTaskAGV.Start_Station) != null)
                            {
                                if (wcsTaskMain.ProcessType == ProcessType.砂芯换车.ToString())
                                {
                                    //查询当前站台需要什么类型的托盘
                                    var plcStation = _SqlSugarDapper.DB.Queryable<WcsDevStation>().Where(w => w.Station_Plc_No == wcsTaskAGV.Start_Station).First();

                                    //_SqlSugarDapper.DB.Insertable(new CallMaterial { CallStation = wcsTaskAGV.Start_Station, MaterialType = plcStation.ProductionMaterials }).ExecuteCommand();
                                }
                            }

                            if (wcsTaskAGV.Replenishment == 1)
                            {
                                //是否是制砂芯缓存站台
                                if (ProcessingTaskMain._processingTaskMain.SandCoreBufferStation.Find(f => f == wcsTaskAGV.Start_Station) != null)
                                {
                                    if (wcsTaskMain.ProcessType == ProcessType.砂芯换车.ToString())
                                    {
                                        //查询当前站台需要什么类型的托盘
                                        var plcStation = _SqlSugarDapper.DB.Queryable<WcsDevStation>().Where(w => w.Station_Plc_No == wcsTaskAGV.Start_Station).First();

                                        _SqlSugarDapper.DB.Insertable(new WCS.Entity.CallMaterial { CallStation = wcsTaskAGV.Start_Station, MaterialType = plcStation.ProductionMaterials }).ExecuteCommand();
                                    }
                                    else
                                    {
                                        // 砂芯缓存补空车
                                        _SqlSugarDapper.DB.Insertable(new WCS.Entity.CallMaterial { CallStation = wcsTaskAGV.Start_Station, MaterialType = wcsTaskAGV.materialType }).ExecuteCommand();
                                    }
                                }
                            }

                            //判断当前是否是砂芯等待站台
                            if (wcsTaskAGV.Start_Station == "1111" || wcsTaskAGV.Start_Station == "1222" ||
                                wcsTaskAGV.Start_Station == "1333" || wcsTaskAGV.Start_Station == "1444")
                            {
                                var Start_Station = _Position.FindFirstKeyByValue(wcsTaskAGV.Start_Station);

                                var task = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().Where(w => w.Start_Station == Start_Station && w.Task_Code != wcsTaskAGV.Task_Code).First();

                                if (task == null || task.Task_State == (int)Task_State.任务完成)
                                {
                                    //告诉AGV允许进入
                                    var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                                    _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                          .SetColumns(w => w.Task_State == (int)Task_State.任务完成)
                                          .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                                          .ExecuteCommand();
                                }

                                continue;
                            }

                            //更新子任务当前位置
                            //更任务的任务提示
                            _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                              .SetColumns(w => w.Task_State == (int)Task_State.任务完成)
                              .Where(w => w.Sub_TaskCode == wcsTaskAGV.Sub_TaskCode)
                              .ExecuteCommand();

                            RunLog.runLog.Debug($"{_modular}-----任务号【{wcsTaskAGV.Task_Code}】【{wcsTaskAGV.Start_Station}】收到AGV的任务完成");

                            if (wcsTaskAGV.Task_Type == "in")
                            {
                                LocationInfo.ChangeStatioState(wcsTaskAGV.Start_Station, StationState.在库);

                                if (!wcsTaskAGV.Start_Station.Contains("100"))
                                {
                                    _SqlSugarDapper.DB.Deleteable<LocationInfo>()
                                    .Where(w => w.StationLocation == wcsTaskAGV.Start_Station).ExecuteCommand();
                                }

                                RunLog.runLog.Debug($"{_modular}空托缓存 修改库存 【在库】 成功 站台号:【{wcsTaskAGV.Start_Station}】");
                            }
                            else if (wcsTaskAGV.Task_Type == "out")
                            {
                                LocationInfo.DeleteLocationInfo(wcsTaskAGV.Start_Station);

                                RunLog.runLog.Debug($"{_modular}空托缓存 删除库存 成功 站台号:【{wcsTaskAGV.Start_Station}】");
                            }
                        }
                    }
                    else
                    {
                        //筛选出正等待在执行的任务
                        wcsTaskAGV = wcsTaskMain.wcsTaskAGVs.Find(p => p.Task_State == (int)Task_State.AGV任务下发);
                        //调用接口下发任务
                        if (_simulation == "0")
                        {
                            AGVWriteData aGVWriteData = new AGVWriteData
                            {
                                taskChain = new TaskChain { areaId = 1, isReturn = 1 },
                                tasks = new List<TaskPo>()
                            };
                            //foreach (var item in wcsTaskMain.wcsTaskAGVs)
                            foreach (var item in _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().Where(w => w.Task_Code == wcsTaskMain.Task_Code).OrderBy(w => w.indexOrder).ToList())
                            {
                                var obj = new
                                {
                                    sub = item.Sub_TaskCode
                                };
                                aGVWriteData.tasks.Add(new TaskPo()
                                {
                                    endPointCode = item.Start_Station,
                                    mapId = 3,
                                    taskType = item.ActionType,
                                    extend = JsonConvert.SerializeObject(obj)
                                });
                            }

                            if (_simulation == "0")
                            {
                                //给调度系统下发任务
                                var result = HttpRequest.httpRequest.WebPostRequest(JsonConvert.SerializeObject(aGVWriteData), _SendTask);

                                RunLog.runLog.Debug($"{_modular}准备下发AGV任务 任务号:【{wcsTaskAGV.Task_Code}】,【JSON】{JsonConvert.SerializeObject(aGVWriteData)}");

                                //给需要调度确认的AGV任务添加任务链ID
                                _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                                    .SetColumns(w => w.AGVTaskID == Convert.ToInt32(result.data))
                                    .Where(w => w.Task_Code == wcsTaskAGV.Task_Code)
                                    .ExecuteCommand();

                                //下发是否成功
                                if (result.errCode == "Err")
                                {
                                    RunLog.runLog.Warning($"{_modular}下发任务失败 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                    continue;
                                }
                                RunLog.runLog.Debug($"{_modular}下发任务成功 任务号:【{wcsTaskAGV.Task_Code}】");
                            }
                        }
                        _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                            .SetColumns(w => w.Task_State == (int)Task_State.执行中)
                            .Where(w => w.Task_Code == wcsTaskAGV.Task_Code)
                            .ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
            }
        }

        /// <summary>
        /// 发送交互步骤
        /// </summary>
        /// <returns></returns>
        public bool SendInteractive(WcsTaskAGV wcsTaskAGV, ConveyorStation activeStation, WCSInteractiveStep step)
        {
            try
            {
                //发送交互步骤
                //创建要发送的数据
                if (step.InteractiveDev == "AGV")
                {
                    //放行
                    if (_simulation == "0")
                    {
                        //告诉AGV允许进入
                        var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                        //下发是否成功
                        if (result.errCode == "Err")
                        {
                            RunLog.runLog.Warning($"{_modular}AGV允许进入放行失败 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                            return false;
                        }
                        // RunLog.runLog.Warning($"{_modular}发送AGV允许进入成功 任务号:【{wcsTaskAGV.Task_Code}】AGV任务ID【{wcsTaskAGV.AGVTaskID}】");
                    }
                    return true;
                }
                else if (step.InteractiveDev == "PLC")
                {
                    #region
                    if (_simulation == "0")
                    {
                        //给PLC发送AGV到位
                        if (step.CurrentState == 30)
                        {
                            if (wcsTaskAGV.Start_Station == "A001")
                            {
                                var s = PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.0", (bool)true);
                                if (s.IsSuccess)
                                {
                                    RunLog.runLog.Debug($@"-----------------------");
                                    RunLog.runLog.Debug($@"{_modular}A001 AGV到位，【DB48.10.0】= True 发送给PLC");
                                    RunLog.runLog.Debug($@"-----------------------");
                                }
                            }
                            if (wcsTaskAGV.Start_Station == "A007")
                            {
                                var s = PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.1", (bool)true);
                                if (s.IsSuccess)
                                {
                                    RunLog.runLog.Debug($@"{_modular}A007 AGV到位，【DB48.10.1】= True 发送给PLC");
                                    RunLog.runLog.Debug($@"-----------------------");
                                }
                            }
                            if (wcsTaskAGV.Start_Station == "B001")
                            {
                                var s = PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.2", (bool)true);
                                if (s.IsSuccess)
                                {
                                    RunLog.runLog.Debug($@"{_modular}B001 AGV到位，【DB48.10.2】= True 发送给PLC");
                                    RunLog.runLog.Debug($@"-----------------------");
                                }
                            }
                            if (wcsTaskAGV.Start_Station == "B007")
                            {
                                var s = PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.3", (bool)true);
                                if (s.IsSuccess)
                                {
                                    RunLog.runLog.Debug($@"{_modular}B007 AGV到位，【DB48.10.3】= True发送给PLC");
                                    RunLog.runLog.Debug($@"-----------------------");
                                }
                            }
                        }
                        //给PLC发送AGV已退出完成
                        if (step.CurrentState == 48)
                        {
                            //告诉AGV放行
                            var result = HttpRequest.httpRequest.WebGetRequest(wcsTaskAGV.AGVTaskID.ToString(), _ConfirmRelease);

                            //下发是否成功
                            if (result.errCode == "Err")
                            {
                                RunLog.runLog.Warning($"{_modular}AGV离开放行失败 任务号:【{wcsTaskAGV.Task_Code}】RCS返回失败信息【{result.errMsg}】");

                                return false;
                            }

                            var PlcNo = "";
                            foreach (var item in _Position)
                            {
                                if (item.Value == wcsTaskAGV.Start_Station)
                                {
                                    PlcNo = item.Key;
                                    break;
                                }
                            }
                            if (PlcNo == "A001")
                            {
                                PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.0", (bool)false);
                                RunLog.runLog.Debug($@"{_modular}A001 AGV离开完成，【DB48.16.0】= True 发送给PLC");
                                RunLog.runLog.Debug($@"-----------------------");
                            }
                            if (PlcNo == "A007")
                            {
                                PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.1", (bool)false);
                                RunLog.runLog.Debug($@"{_modular}A007 AGV离开完成，【DB48.16.1】= True 发送给PLC");
                                RunLog.runLog.Debug($@"-----------------------");
                            }
                            if (PlcNo == "B001")
                            {
                                PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.2", (bool)false);
                                RunLog.runLog.Debug($@"{_modular}B001 AGV离开完成，【DB48.16.2】= True 发送给PLC");
                                RunLog.runLog.Debug($@"-----------------------");
                            }
                            if (PlcNo == "B007")
                            {
                                PlcDev._plcDev.s7Plc.S7PlcWiterBool("DB48.16.3", (bool)false);
                                RunLog.runLog.Debug($@"{_modular}B007 AGV离开完成，【DB48.16.3】= True 发送给PLC");
                                RunLog.runLog.Debug($@"-----------------------");
                            }
                        }
                    }
                    #endregion
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
                return false;
            }
        }

        /// <summary>
        /// 接受交互步骤
        /// </summary>
        /// <returns></returns>
        public bool ReceiveInteractive(WcsTaskAGV wcsTaskAGV, ConveyorStation activeStation, WCSInteractiveStep step)
        {
            try
            {
                //等待PLC的允许进入信号
                if (wcsTaskAGV.InteractiveState == 14)
                {

                }

                //等待PLC的放货完成、取货完成信号
                if (wcsTaskAGV.InteractiveState == 34)
                {
                    if (wcsTaskAGV.Start_Station == "A001" && PlcDev._plcDev.conveyorReadData.GetAOne == true)
                    {
                        RunLog.runLog.Debug($@"{_modular} A001 PLC的取货完成");
                        return true;
                    }
                    if (wcsTaskAGV.Start_Station == "A007" && PlcDev._plcDev.conveyorReadData.GetASeven == true)
                    {
                        RunLog.runLog.Debug($@"{_modular} A007 PLC的取货完成");
                        return true;
                    }
                    if (wcsTaskAGV.Start_Station == "B001" && PlcDev._plcDev.conveyorReadData.GetBOne == true)
                    {
                        RunLog.runLog.Debug($@"{_modular} B001 PLC的放货完成");
                        return true;
                    }
                    if (wcsTaskAGV.Start_Station == "B007" && PlcDev._plcDev.conveyorReadData.GetBSeven == true)
                    {
                        RunLog.runLog.Debug($@"{_modular} B007 PLC的放货完成");
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
                return false;
            }
        }

        /// <summary>
        /// AGV任务完成
        /// </summary>
        /// <param name="end_Station"></param>
        /// <param name="conveyTask"></param>
        private void AGVTaskCompleteAll(WcsTaskAGV wcsTaskAGV)
        {
            try
            {
                var WcsTaskAGVList = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().Where(w => w.Task_Code == wcsTaskAGV.Task_Code).ToList();

                foreach (var item in WcsTaskAGVList)
                {
                    //添加历史任务
                    _SqlSugarDapper.DB.Insertable<WcsZBKTaskAGV>(item).ExecuteCommand();
                }

                //删除子任务
                _SqlSugarDapper.DB.Deleteable<WcsTaskAGV>().Where(w => w.Task_Code == wcsTaskAGV.Task_Code).ExecuteCommand();

                //更新主任务状态成上报完成
                _SqlSugarDapper.DB.Updateable<WcsTaskMain>()
                      .SetColumns(w => w.Task_State == (int)Task_State.上报WMS任务完成)
                      .Where(w => w.Task_Code == wcsTaskAGV.Task_Code)
                      .ExecuteCommand();

            }
            catch (Exception ex)
            {
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
            }
        }

        /// <summary>
        /// 删除AGV任务并加入历史记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="TaskCode"></param>
        public void DeleteAGVTask(string Task_Code, string TaskTips)
        {
            try
            {
                //更任务的任务提示
                _SqlSugarDapper.DB.Updateable<WcsTaskAGV>()
                    .SetColumns(w => w.TaskTips == TaskTips)
                    .Where(w => w.Task_Code == Task_Code)
                    .ExecuteCommand();

                var wcsTaskAGV = _SqlSugarDapper.DB.Queryable<WcsTaskAGV>().Where(w => w.Task_Code == Task_Code).First();

                if (wcsTaskAGV != null)
                {
                    WcsZBKTaskAGV wcsZBKTaskAGV = new WcsZBKTaskAGV();
                    wcsZBKTaskAGV.MapperFrom(wcsTaskAGV);

                    //添加历史记录
                    _SqlSugarDapper.DB.Insertable(wcsZBKTaskAGV).ExecuteCommand();

                    //删除子任务
                    _SqlSugarDapper.DB.Deleteable<WcsTaskAGV>()
                       .Where(w => w.Task_Code == Task_Code)
                       .ExecuteCommand();
                }
            }
            catch (Exception ex)
            {
                RunLog.runLog.Error($@"{_modular}异常信息【{ex.Message}】【{ex.StackTrace}】");
            }
           
        }

        /// <summary>
        /// 获取两个入库口那个物料最少
        /// </summary>
        public string GetStoragePortTask(string type, int StartStation = 0)
        {
            string station1;
            string station2;
            StartStation BufferStation = null;
            if (type == "in")
            {
                station1 = "A001";
                station2 = "A007";

                BufferStation = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT START_STATION from  WCS_TASK_AGV where START_STATION = '{station1}' or START_STATION = '{station2}' GROUP BY START_STATION ORDER BY count(1) desc").ToList().FirstOrDefault();

                if (BufferStation == null)
                {
                    return station1;
                }
                return BufferStation.START_STATION = BufferStation.START_STATION == station1 ? station2 : station1;
            }
            else
            {
                //满托可以去 B001 和 B007
                //if (StartStation == 1)
                //{
                //    var B001TaskCount = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT END_STATION from  WCS_TASK_Conveyor where End_Station = 'B001'").ToList();
                //    var B007TaskCount = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT END_STATION from  WCS_TASK_Conveyor where End_Station = 'B007'").ToList();

                //    if (B001TaskCount.Count < 4)
                //    {
                //        return "B001";
                //    }

                //    if ((B007TaskCount.Count / B001TaskCount.Count) < 0.5)
                //    {
                //        bool record = true;
                //        var warehouseInfos = _SqlSugarDapper.DB.SqlQueryable<WarehouseInfo>($@"SELECT ID FROM WCS_Warehouse WHERE IsFullMaterial = 0").ToList();
                //        foreach (var item in warehouseInfos)
                //        {
                //            var werhousCode = item.ID.Split('-')[0];
                //            var BinCode = item.ID.Split('-')[1];
                //            //B007没有空托出库的任务
                //            var task = _SqlSugarDapper.DB.Queryable<WcsTaskConveyor>().Where(w => w.Task_Type == "out" && w.Start_Station == werhousCode && w.WMSBinCode == BinCode).ToList();

                //            //有的话就不去B007
                //            if (task.Count != 0)
                //            {
                //                record = false;
                //                break;
                //            }
                //        }
                //        if (record)
                //        {
                //            return "B007";
                //        }
                //    }
                //    return "B001";
                //}
                //else
                //{
                //    //空托只去 B007
                //    return "B007";
                //}

                station1 = "B001";
                station2 = "B007";

                if (StartStation == 1)
                {
                    return station2;
                }

                var warehouseInfos = _SqlSugarDapper.DB.SqlQueryable<WarehouseInfo>($@"SELECT ID FROM WCS_Warehouse WHERE IsFullMaterial = 0").ToList();
                foreach (var item in warehouseInfos)
                {
                    var werhousCode = item.ID.Split('-')[0];
                    var BinCode = item.ID.Split('-')[1];
                    //B007有空托出库的任务
                    var task = _SqlSugarDapper.DB.Queryable<WcsTaskConveyor>().Where(w => w.Task_Type == "out" && w.Start_Station == werhousCode && w.WMSBinCode == BinCode).ToList();

                    //有的话就去B001
                    if (task.Count != 0)
                    {
                        return station1;
                    }
                }

                BufferStation = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT END_STATION from  WCS_TASK_Conveyor where END_STATION = '{station1}' or END_STATION = '{station2}' GROUP BY END_STATION ORDER BY count(1) desc,END_STATION desc").ToList().FirstOrDefault();

                if (BufferStation == null)
                {
                    return station1;
                }
                return BufferStation.END_STATION = BufferStation.END_STATION == station1 ? station2 : station1;
            }
        }

        public string GetFreeStation(int StartStation=0)
        {
            string station1;
            string station2;
            StartStation BufferStation = null;
            station1 = "4008";
            station2 = "4009";

            BufferStation = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT START_STATION from  WCS_TASK_AGV where START_STATION = '{station1}' or START_STATION = '{station2}' GROUP BY START_STATION ORDER BY count(1) desc").ToList().FirstOrDefault();

            if (BufferStation == null)
            {
                return station1;
            }
            return BufferStation.START_STATION = BufferStation.START_STATION == station1 ? station2 : station1;
        }

        //public string GetStoragePortTask(string type)
        //{
        //    string station1;
        //    string station2;
        //    if (type == "in")
        //    {
        //        station1 = "A001";
        //        station2 = "A007";
        //    }
        //    else
        //    {
        //        station1 = "B001";
        //        station2 = "B007";
        //    }
        //    var BufferStation = _SqlSugarDapper.DB.SqlQueryable<StartStation>($"SELECT START_STATION from  WCS_TASK_AGV where START_STATION = '{station1}' or START_STATION = '{station2}' GROUP BY START_STATION ORDER BY count(1)").ToList().FirstOrDefault();

        //    if (BufferStation == null)
        //    {
        //        return station1;
        //    }
        //    return BufferStation.START_STATION;
        //}

        /// <summary>
        /// 把小车拉走入库
        /// </summary>
        /// <param name="station"></param>
        public void CarIn(string station, string material)
        {
            //创建回库任务
            WcsTaskMain wcsTaskMain1 = new WcsTaskMain
            {
                Task_Type = "in",
                TrayBar_Code = "111111",
                Start_Station = station,
                End_Station = GetStoragePortTask("in"),//那个入库口任务少就去那个
                Current_Station = station,
                MaterialType = material,
                ProcessType = ProcessType.砂芯换车.ToString(),
            };
            _SqlSugarDapper.DB.Insertable(wcsTaskMain1).ExecuteCommand();
        }

        public static string GetCacheBit(string areaCode, int binCode)
        {
            //查找可用库位
            return _SqlSugarDapper.DB.Queryable<AreaLocation>()
                .Where(w => w.AreaCode == areaCode && w.BinCode == binCode && w.Enable == "1"&& SqlFunc.Subqueryable<LocationInfo>().Where(f => w.StationNo == f.StationLocation).NotAny()
                ).First()?.StationNo;
        }
    }

    public class StartStation
    {
        public string START_STATION { get; set; }

        public string END_STATION { get; set; }
    }
}
